package com.ruoyi.web.controller.business.sundry;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbNotification;
import com.ruoyi.business.sundry.service.JbNotificationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 消息通知 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("消息通知")
@RestController
@RequestMapping("/notification")
public class JbNotificationController extends BaseController
{
    @Autowired
    private JbNotificationService service;

    /**
     * 查询消息通知列表
     */
    @ApiOperation("查询消息通知列表")
    // @PreAuthorize("@ss.hasPermi('notification:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbNotification jbNotification)
    {
        Long userId = getUserId();
        LambdaQueryWrapper<JbNotification> select = new LambdaQueryWrapper<>();
        select.eq(JbNotification::getRecipientId, userId);
        IPage<JbNotification> iPage = service.page(page.b(), select);
        return JoR.getDataTable(iPage);
    }

    /**
     * 导出消息通知列表
     */
    @ApiOperation("导出消息通知列表")
    // @PreAuthorize("@ss.hasPermi('notification:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbNotification jbNotification)
    {
        LambdaQueryWrapper<JbNotification> select = new LambdaQueryWrapper<>();
        IPage<JbNotification> iPage = service.page(page.b(), select);

        List<JbNotification> list = iPage.getRecords();
        ExcelUtil<JbNotification> util = new ExcelUtil<JbNotification>(JbNotification.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    /**
     * 获取消息通知详细信息
     */
    @ApiOperation("获取消息通知详细信息")
    // @PreAuthorize("@ss.hasPermi('notification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        JbNotification byId = service.getById(id);
        JbNotification updateStatus = new JbNotification();
        updateStatus.setStatus(Constants.PUBLIC_NUMBER_0.toString());
        updateStatus.setId(id);
        updateStatus.setReceiveTime(new Date());
        service.updateById(updateStatus);
        return success(byId);
    }

    /**
     * 新增消息通知
     */
    @ApiOperation("新增消息通知")
    // @PreAuthorize("@ss.hasPermi('notification:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbNotification jbNotification)
    {
        return toAjax(service.save(jbNotification));
    }

    /**
     * 修改消息通知
     */
    @ApiOperation("修改消息通知")
    // @PreAuthorize("@ss.hasPermi('notification:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbNotification jbNotification)
    {
        return toAjax(service.updateById(jbNotification));
    }

    /**
     * 删除消息通知
     */
    @ApiOperation("删除消息通知")
    // @PreAuthorize("@ss.hasPermi('notification:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 设置已读
     */
    @ApiOperation("设置已读")
    // @PreAuthorize("@ss.hasPermi('notification:updateStatus')")
    @Log(title = "设置已读", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus/{ids}")
    public AjaxResult updateStatus(@PathVariable Long[] ids)
    {
        JbNotification updateStatus = new JbNotification();
        updateStatus.setStatus(Constants.PUBLIC_NUMBER_0.toString());
        updateStatus.setReceiveTime(new Date());
        return toAjax(service.update(updateStatus, Wrappers.lambdaUpdate(JbNotification.class)
                .in(JbNotification::getId,ids)));
    }
}
